﻿Imports Mystery

Public Class DefaultStartScreenControl
    Implements ITypeShower, IContentCreatorControl

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim sn As IStartNavigator = getGlobalObject(Of IStartNavigator)()
        For Each important As IButton In sn.getMostImportantButtons()
            Me.panel.Children.Add(important.getSquareButton(200))
        Next

        ' Add any initialization after the InitializeComponent() call.
        For Each other_button As IButton In sn.getAlltheButtons
            MoreMenu.Items.Add(other_button.getMenuItem())
        Next

        For Each newButton As IButton In sn.getNewContentButtons()
            NewMenu.Items.Add(newButton.getMenuItem())
        Next


        For Each mitem As MenuItem In sn.getAdditionalMenuItems
            myMainMenu.Items.Add(mitem)
        Next

        Dim ir As InterfaceStatus = getGlobalObject(Of InterfaceStatus)()
        ir.registerTypeShow(Me)
        ir.registerTypeAdd(Me)

        userEvents.load()
    End Sub

    Private _currently_showen As New Dictionary(Of Type, TabItem)

    Public Function show(content_type As Type) As Boolean Implements ITypeShower.show
        If content_type Is Nothing Then
            start.Focus()
            Return True
        End If
        If _currently_showen.ContainsKey(content_type) Then
            _currently_showen(content_type).Focus()
        Else
            Dim new_tab As New TabItem
            new_tab.Name = content_type.getMysteryAttribute(Of ContentType).name
            _currently_showen(content_type) = new_tab
            Dim sp As New StackPanel With {.Orientation = Orientation.Horizontal, .Height = 30}

            sp.Children.Add(New Image With {.Source = New BitmapImage(New Uri(getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentTypeButtonAttribute)(content_type).normal_icon, UriKind.RelativeOrAbsolute))})
            'sp.Children.Add(getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentButtonAttribute)(content_type).createButton(Nothing).getImage)
            sp.Children.Add(New Label With {.Content = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentType)(content_type).list_label})
            new_tab.Header = sp
            Dim tab_content As UserControl = getMystery.AssemblyRegister.getMysteryClassAttribute(Of ContentListAttr)(content_type).createControl()
            new_tab.Content = tab_content
            AddHandler new_tab.GotFocus, Sub()
                                             Dim istatus As InterfaceStatus = getGlobalObject(Of InterfaceStatus)()
                                             istatus.showen_type = content_type
                                             If TypeOf (tab_content) Is IContentShower Then
                                                 istatus.showen_content = DirectCast(tab_content, IContentShower).showen_content
                                             End If
                                         End Sub
            tabs.Items.Add(new_tab)
            _currently_showen(content_type).Focus()
        End If
        Return True
    End Function

    Public Function canDo(content_type As System.Type) As Boolean Implements IContentCreatorControl.canDo
        Return True
    End Function

    Dim new_window As Window

    Public Function setUp(content_type As System.Type, content As IContent) As Boolean Implements IContentCreatorControl.setUp
        If new_window Is Nothing Then
            new_window = New Window
            new_window.SizeToContent = SizeToContent.WidthAndHeight
            new_window.WindowStartupLocation = WindowStartupLocation.CenterScreen
            AddHandler new_window.Closed, Sub() new_window = Nothing
        End If

        Dim cat As ContentControlAddAttribute = content_type.getMysteryAttribute(Of ContentControlAddAttribute)()
        Dim control As UserControl = cat.createControl(content)
        Dim creator As IContentCreatorControl = DirectCast(control, IContentCreatorControl)
        AddHandler creator.created, AddressOf Me.contentCreated
        new_window.Content = control
        new_window.Show()
        new_window.Focus()
        Return True
    End Function


    Private Sub contentCreated(content As IContent)
        If new_window IsNot Nothing Then new_window.Close()

        RaiseEvent created(content)
        Dim ir As InterfaceStatus = getGlobalObject(Of InterfaceStatus)()
        If Not ir.showContent(content, False) Then
            'type first
            ir.showType(content.GetType(), force_reload:=False)
            ir.showContent(content, False)
        End If
    End Sub

    Public Event created(content As Mystery.IContent) Implements IContentCreatorControl.created

    Private Sub start_GotFocus(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles start.GotFocus
        getGlobalObject(Of InterfaceStatus)().showen_type = Nothing
        getGlobalObject(Of InterfaceStatus)().showen_content = Nothing
    End Sub

    Private Sub tabs_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles tabs.SelectionChanged
        If start.IsSelected Then
            getGlobalObject(Of InterfaceStatus)().showen_type = Nothing
            getGlobalObject(Of InterfaceStatus)().showen_content = Nothing
        End If
    End Sub

    Private Sub tbSearch_KeyUp(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles tbSearch.KeyUp
        If tbSearch.Text.Length > 2 Then
            lSearchResult.Items.Clear()
            Dim search_text As String = tbSearch.Text.Trim.ToLower
            Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
            Dim contents As IEnumerable(Of IContent) = cd.search(search_text)
            If contents.Count = 0 Then
                lSearchResult.Visibility = Windows.Visibility.Hidden
            Else
                lSearchResult.Visibility = Windows.Visibility.Visible
            End If
            For Each c As IContent In contents
                Dim cb As New Button
                Dim icb As IContentButton = c.GetType.getMysteryAttribute(Of ContentButtonAttribute)().createButton(c)
                AddHandler cb.Click, AddressOf icb.Press
                cb.Content = icb.getText
                cb.MinWidth = 100
                Dim image As Image = icb.getImage
                image.Height = 30
                image.Width = 30
                Dim sp As New StackPanel With {.Orientation = Orientation.Horizontal}
                sp.Children.Add(image)
                sp.Children.Add(cb)
                lSearchResult.Items.Add(sp)
            Next
        Else
            lSearchResult.Items.Clear()
            lSearchResult.Visibility = Windows.Visibility.Hidden
        End If
    End Sub
End Class
